package com.zp.resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.order.ResultScheduleVo;
import com.zp.bean.order.ScheduleVo;
import com.zp.domain.Schedule;
import com.zp.domain.ScheduleType;
import com.zp.domain.User;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * schedule services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/24
 * 
 */
@Component
@Path("/rest/schedule")
@Singleton
public class ScheduleServices extends BaseServices {
	/**
	 * add schedule path:/schedule/addSchedule Http:POST
	 * 
	 * @param schedule
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@POST
	@Path("/addSchedule")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addSchedule(Schedule schedule) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.save(schedule);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update Schedule information path:/schedule/updateSchedule Http:PUT
	 * 
	 * @param schedule
	 * 
	 * @return Response,ResultScheduleVo with JSON String like:
	 */
	@PUT
	@Path("/updateSchedule")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateSchedule(Schedule schedule) {
		ResultScheduleVo result = new ResultScheduleVo();
		try {
			this.jpaDao.update(schedule);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			ScheduleVo vo = new ScheduleVo();
			vo.setId(schedule.getId());
			vo.setScheduleDate(schedule.getScheduleDate());
			vo.setSt((ScheduleType) this.jpaDao.findById(ScheduleType.class,
					schedule.getId()));
			vo.setStatus(schedule.getStatus());
			vo.setUser((User) this.jpaDao.findById(User.class,
					schedule.getUserId()));
			result.setSchedule(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get schedule by id path:/type/getSchedule Http:GET
	 * 
	 * @param id
	 *            primary key of Schedule
	 * 
	 * @return Response,ResultScheduleVo with JSON String like:
	 */
	@GET
	@Path("/getSchedule")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getScheduleById(@QueryParam("id") String id) {
		logger.info("getScheduleById called id:" + id);
		ResultScheduleVo result = new ResultScheduleVo();
		try {
			Schedule schedule = (Schedule) jpaDao.findById(Schedule.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			ScheduleVo vo = new ScheduleVo();
			vo.setId(schedule.getId());
			vo.setScheduleDate(schedule.getScheduleDate());
			vo.setSt((ScheduleType) this.jpaDao.findById(ScheduleType.class,
					schedule.getId()));
			vo.setStatus(schedule.getStatus());
			vo.setUser((User) this.jpaDao.findById(User.class,
					schedule.getUserId()));
			result.setSchedule(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete Schedule by id path:/schedule/deleteSchedule Http:DELETE
	 * 
	 * @param id
	 *            primary key of Schedule
	 * 
	 * @return Response,ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deleteSchedule")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteSchedule(@QueryParam("id") String id) {
		logger.info("deleteSchedule called id:" + id);
		ResultCommonVo result = new ResultCommonVo();
		try {
			this.jpaDao.deleteById(Schedule.class, id);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * search schedule with filter parameters path:/schedule/searchSchedule
	 * Http:GET
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @param content
	 *            the id of user or the code of Type,if ignored the value is
	 *            null
	 * 
	 * @return Response, ResultScheduleVo with JSON String like:
	 */
	@GET
	@Path("/searchSchedule")
	@Produces(MediaType.APPLICATION_JSON)
	public Response searchSchedule(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("content") String content) {
		logger.info("searchSchedule called json pageNo is:" + pageNo
				+ " pageSize is:" + pageSize + " content:" + content);
		String jpql = "from Schedule where userId=? or typeName=? order by scheduleDate desc";
		String countSql = "select count(id)from Schedule where userId=? or typeName=?";
		List<?> list = null;
		ResultScheduleVo result = new ResultScheduleVo();
		List<ScheduleVo> lt = new ArrayList<ScheduleVo>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(content) || content == null) {
			jpql = "from Schedule";
			countSql = "select count(id)from Schedule";
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, null);
				result.setTotalCount(this.jpaDao.getCount(countSql, null));
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}

		} else {
			Object[] param = { content, content };
			try {
				list = this.jpaDao.getResultData(Constant.NEGATIVE_ONE,
						Constant.NEGATIVE_ONE, jpql, param);
				result.setTotalCount(this.jpaDao.getCount(countSql, param));

			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Schedule schedule = (Schedule) o;
				ScheduleVo vo = new ScheduleVo();
				vo.setId(schedule.getId());
				vo.setScheduleDate(schedule.getScheduleDate());
				try {
					vo.setSt((ScheduleType) this.jpaDao.findById(
							ScheduleType.class, schedule.getId()));
				} catch (MyException e) {
					vo.setSt(null);
				}
				try {
					vo.setUser((User) this.jpaDao.findById(User.class,
							schedule.getUserId()));
				} catch (MyException e) {
					vo.setUser(null);
				}
				vo.setStatus(schedule.getStatus());
				lt.add(vo);
			}
			result.setList(lt);

		}
		return Response.ok(result).build();
	}

}
